import Vue from 'vue'
import Vuex from 'vuex'
import { getPetList } from '@/api/pet'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 宠物信息列表
    petList: JSON.parse(sessionStorage.getItem('petList') || '[]'),
    // 每个宠物的详细信息
    petData: JSON.parse(sessionStorage.getItem('petData') || '{}'),
    user: JSON.parse(localStorage.getItem('user') || '{}'),
    admin: JSON.parse(localStorage.getItem('admin') || '{}'),
  },
  getters: {},
  mutations: {
    setPetList(state, data) {
      state.petList = data
      sessionStorage.setItem('petList', JSON.stringify(data))
    },
    setPetData(state, data) {
      state.petData = data
      sessionStorage.setItem('petData', JSON.stringify(data))
    },
    setUser(state, data) {
      state.user = data
      localStorage.setItem('user', JSON.stringify(data))
    },
    setAdmin(state, data) {
      state.admin = data
      localStorage.setItem('admin', JSON.stringify(data))
    },
  },
  actions: {
    async getPetInfoList({ commit }) {
      const result = await getPetList()
      commit('setPetList', result.data)
    },
  },
  modules: {},
})
